package helpers;

import helpers.ITaskManagerFactory;
import Model.TaskManager;

/**
 * Singleton abstract factory for creation of TaskManager models
 * 
 * @author psyomn
 */
public class TaskManagerFactory implements ITaskManagerFactory
{
	private static ITaskManagerFactory mInst = null;

	public static synchronized ITaskManagerFactory getInst()
	{
		Configuration.inst();
		String str = Configuration.getAttribute("factory");
		Class<?> c = null;

		System.out.println("str equals: " + str);
		try
		{

			if (mInst == null)
			{
				if (str.equalsIgnoreCase("mock"))
				{
					CustomLogger.getInst().log("Using the MOCK factory");
					c = Class.forName("helpers.MockFactory");
				} 
				else if (str.equalsIgnoreCase("production"))
				{
					CustomLogger.getInst().log("Using the PRODUCTION factory");
					c = Class.forName("helpers.ProductionFactory");
				} 
				else
				{
					throw new Exception("Misconfigured attribute in configuration.txt");
				}

				mInst = (ITaskManagerFactory) c.newInstance();
			}

		} 
		catch (Exception e)
		{
			e.printStackTrace();
		}

		return mInst;
	}

	/**
	 * Return a new task manager, depending on what you specified in
	 * configuration.txt
	 * 
	 * @throws Exception
	 */
	public TaskManager getNewTaskManager() throws Exception
	{
		if (mInst == null)
			getInst();
		return mInst.getNewTaskManager();
	}
}
